#pragma once

#include <mutex>
#include <pthread.h>

// Mutex 类是对 pthread_mutex_t 的封装
class Mutex
{
public:
    Mutex(pthread_mutex_t *mtx):_pmtx(mtx)
    {}

    ~Mutex()
    {}

    void lock()
    {
        pthread_mutex_lock(_pmtx);
    }

    void unlock()
    {
        pthread_mutex_unlock(_pmtx);
    }

private:
    pthread_mutex_t* _pmtx;
};

// RAII 加锁方式
// lockGuard 实现了自动释放互斥锁的效果
class lockGuard
{
public:
    lockGuard(pthread_mutex_t *mtx):_mtx(mtx)
    {
        _mtx.lock();
    }

    ~lockGuard()
    {
        _mtx.unlock();
    }

private:
    Mutex _mtx;
};